/*  
 * i-OSGi - Tunable Bundle Isolation for OSGi
 * Copyright (C) 2011  Sven Schulz
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.iosgi.servicebus;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author Sven Schulz
 */
public class ServiceDescriptor {

	private InetAddress host;
	private int port;
	private long serviceId;

	public ServiceDescriptor(InetAddress host, int port, long serviceId) {
		this.host = host;
		this.port = port;
		this.serviceId = serviceId;
	}

	public ServiceDescriptor(String s) {
		String[] parts = s.split(":");
		try {
			host = InetAddress.getByName(parts[0]);
		} catch (UnknownHostException e) {
			throw new RuntimeException(e);
		}
		port = Integer.parseInt(parts[1]);
		serviceId = Long.parseLong(parts[2]);
	}

	public InetAddress getHost() {
		return host;
	}

	public int getPort() {
		return port;
	}

	public long getServiceId() {
		return serviceId;
	}

	@Override
	public String toString() {
		return host.getHostAddress() + ":" + port + ":" + serviceId;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null || !(obj instanceof ServiceDescriptor)) {
			return false;
		}
		ServiceDescriptor other = (ServiceDescriptor) obj;
		return this.host.equals(other.host) && this.port == other.port
				&& this.serviceId == other.serviceId;
	}

	@Override
	public int hashCode() {
		return host.hashCode() + port + (int) serviceId;
	}

}